// Checkr
// ======
//
// Copyright Gilbert Pellegrom 2008
// This code is provided as is, bugs are probable, no responsibility accepted.
// http://www.gilbertpellegrom.co.uk

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Windows.Forms;

namespace Checkr
{
    public class ProfileManager
    {
        
        string directory = Form1.BASE_DIR + "/profiles";

        public ProfileManager()
        {
            try
            {
                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        public void addProfile(string type, string server, string username, string password, string port, bool ssl)
        {
            FileStream stream = new FileStream(directory + "/" + username + "@" + server + ".pro", 
                                                    FileMode.OpenOrCreate, FileAccess.Write);

            DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
            cryptic.Key = ASCIIEncoding.ASCII.GetBytes("cHeCkRrr");
            cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ChEcKrRR");
            CryptoStream crStream = new CryptoStream(stream, cryptic.CreateEncryptor(),CryptoStreamMode.Write);
            
            byte[] data = ASCIIEncoding.ASCII.GetBytes(type + "\n");
            crStream.Write(data, 0, data.Length);
            data = ASCIIEncoding.ASCII.GetBytes(server + "\n");
            crStream.Write(data, 0, data.Length);
            data = ASCIIEncoding.ASCII.GetBytes(username + "\n");
            crStream.Write(data, 0, data.Length);
            data = ASCIIEncoding.ASCII.GetBytes(password + "\n");
            crStream.Write(data, 0, data.Length);
            data = ASCIIEncoding.ASCII.GetBytes(port + "\n");
            crStream.Write(data, 0, data.Length);
            data = ASCIIEncoding.ASCII.GetBytes(ssl.ToString());
            crStream.Write(data, 0, data.Length);

            crStream.Close();
            stream.Close();
        }

        public void deleteProfile(string file)
        {
            File.Delete(directory + "/" + file + ".pro");
            if (File.Exists(directory + "/" + file + ".dat"))
            {
                File.Delete(directory + "/" + file + ".dat");
            }
            Console.WriteLine("Profile Deleted SucacessFully");
        }

        public ArrayList loadProfiles()
        {
            ArrayList profiles = new ArrayList();
            try
            {
                if (Directory.Exists(directory))
                {
                    foreach (string fiTemp in Directory.GetFiles(directory, "*.pro"))
                    {
                        Profile newProfile = new Profile();

                        FileStream stream = new FileStream(fiTemp, FileMode.Open, FileAccess.Read);
                        DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
                        cryptic.Key = ASCIIEncoding.ASCII.GetBytes("cHeCkRrr");
                        cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ChEcKrRR");
                        CryptoStream crStream = new CryptoStream(stream, cryptic.CreateDecryptor(), CryptoStreamMode.Read);
                        StreamReader sr = new StreamReader(crStream);

                        string data = sr.ReadToEnd();
                        string[] lines = data.Split('\n');
                        newProfile.type = lines[0];
                        newProfile.server = lines[1];
                        newProfile.username = lines[2];
                        newProfile.password = lines[3];
                        newProfile.port = lines[4];
                        string ssl = lines[5];
                        if (ssl == "True")
                            newProfile.ssl = true;
                        else
                            newProfile.ssl = false;

                        sr.Close();
                        stream.Close();

                        profiles.Add(newProfile);
                    }
                }
                else
                {
                    Console.WriteLine("Error: Invalid directory");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            return profiles;
        }

        public Profile loadProfile(string profile)
        {
            Profile newProfile = new Profile();

            try
            {
                FileStream stream = new FileStream(directory + "/" + profile + ".pro", FileMode.Open, FileAccess.Read);
                DESCryptoServiceProvider cryptic = new DESCryptoServiceProvider();
                cryptic.Key = ASCIIEncoding.ASCII.GetBytes("cHeCkRrr");
                cryptic.IV = ASCIIEncoding.ASCII.GetBytes("ChEcKrRR");
                CryptoStream crStream = new CryptoStream(stream, cryptic.CreateDecryptor(), CryptoStreamMode.Read);
                StreamReader sr = new StreamReader(crStream);

                string data = sr.ReadToEnd();
                string[] lines = data.Split('\n');
                newProfile.type = lines[0];
                newProfile.server = lines[1];
                newProfile.username = lines[2];
                newProfile.password = lines[3];
                newProfile.port = lines[4];
                string ssl = lines[5];
                if (ssl == "True")
                    newProfile.ssl = true;
                else
                    newProfile.ssl = false;

                sr.Close();
                stream.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(profile + ".pro");
                Console.WriteLine(e.StackTrace);
            }

            return newProfile;
        }
    }

    public class Profile
    {
        public string type;
        public string server;
        public string username;
        public string password;
        public string port;
        public bool ssl;
    }
}
